/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjs.liyq.web;

import edu.fjs.liyq.entity.Cart;
import edu.fjs.liyq.entity.Product;
import edu.fjs.liyq.entity.User;
import edu.fjs.liyq.service.OrderService;
import edu.fjs.liyq.service.ProductService;
import edu.fjs.liyq.service.impl.OrderServiceImpl;
import edu.fjs.liyq.service.impl.ProductServiceImpl;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author danny
 */
public class CartServlet extends HttpServlet {
    
    public static final String CART_KEY = "ShoppingCart";
    ProductService productService = new ProductServiceImpl();
    OrderService orderService = new OrderServiceImpl();

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //在session中取购物车实例，如果不存在则创建购物车实例，并放在session
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute(CART_KEY);
        if (cart == null) {
            cart = new Cart();
            session.setAttribute(CART_KEY, cart);
        }
        
        // 根据action进行不同的页面逻辑处理
        String action = request.getParameter("action");
        if ("add".equals(action)) {
            // 获取商品id
            String strProdId = request.getParameter("prodId");
            Product product = productService.getById(Long.valueOf(strProdId));
            cart.add(product);
            response.sendRedirect(request.getContextPath() + "/cart?action=list");
        } else if ("list".equals(action)) {
            request.getRequestDispatcher("/WEB-INF/jsp/cart.jsp").forward(request, response);
        } else if ("order".equals(action)) {
            // 先判断是否登录过
            User loginUser = (User) session.getAttribute("LOGIN_USER");
            if (loginUser == null) {
                response.sendRedirect(request.getContextPath()+"/login.do?action=loginForm");
            } else {
                cart.setUser(loginUser);
                boolean placeOrder = orderService.placeOrder(cart);
                if (placeOrder) {
                    // 清空购物车
                    session.removeAttribute(CART_KEY);
                    response.sendRedirect(request.getContextPath()+"/cart?action=list");
                } else {
                    request.setAttribute("error", "系统繁忙，请稍后重试！");
                    request.getRequestDispatcher("/cart?action=list").forward(request, response);
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
